using System;
using System.Collections.Generic;

namespace Processors
{
    /// <summary>
    /// 上下文
    /// </summary>
    public class Context
    {
        private readonly Dictionary<string, object> _keyMap = new Dictionary<string, object>();

        public void Add(string key, object value)
        {
            if (_keyMap.ContainsKey(key))
                throw new ArgumentException($"key值已经存在{key}");
            _keyMap.Add(key, value);
        }

        public void Remove(string key)
        {
            if (!ContainsKey(key))
                throw new ArgumentException($"key值不存在{key}");
            _keyMap.Remove(key);
        }

        public void Modify(string key, object value)
        {
            _keyMap[key] = value;
        }

        public bool ContainsKey(string key)
        {
            return _keyMap.ContainsKey(key);
        }

        public virtual object Get(string key)
        {
            if (!ContainsKey(key))
                throw new KeyNotFoundException($"key值不存在{key}");
            return _keyMap[key];
        }
        
        public virtual T Get<T>(string key)
        {
            if (!ContainsKey(key))
                throw new KeyNotFoundException($"key值不存在{key}");
            return (T)_keyMap[key];
        }
    }
}